package com.lxh.platform.concurrent;

import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;

/**
 * @create: 2021-06-01 11:39
 * @author: xiaoh.luo001@bkjk.com
 * @description:
 **/
public class TestSemaphore {

    public static void main(String[] args) {
        var executorService = Executors.newCachedThreadPool();
        final var semaphore = new Semaphore(5, true);
        for (int index = 0; index < 30; index++) {
            final int NO = index;
            executorService.submit(() -> {
                try {
                    semaphore.acquire();
                    System.out.println("Accessing: " + NO);
                    Thread.sleep((1000));
                    semaphore.release();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
            System.out.println("------" + semaphore.availablePermits());
        }
        executorService.shutdown();
    }
}
